package com.hidtechs.popfax.view;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import javax.net.ssl.HttpsURLConnection;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;

import com.hidtechs.popfax.controller.Constants;
import com.hidtechs.popfax.controller.Requestor;

public abstract class Base extends Activity implements Runnable{
	
	public HashMap<String,String> parameterValues;
	String PREFS="preferences";
	public String userName_shared_prefernece=null;
	public String passWord_shared_prefernece=null;
	protected Handler handler;
	protected ArrayList arrayOfObjectOfParsedData;
	protected HttpsURLConnection conn=null;
	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

	
    abstract void setMapValues();
    
   
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("In Run");
		openConnection();
    	Requestor requestorForXMLParsing= new Requestor();
		arrayOfObjectOfParsedData=requestorForXMLParsing.requestData(parameterValues,conn);
		
		handler.sendEmptyMessage(0);
		
	}
    
    
	protected void openConnection()
	{
		try{
		URL siteUrl= new URL(Constants.WEBSERVER_URL);
		conn= (HttpsURLConnection)siteUrl.openConnection();
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		conn.setDoInput(true);
		}catch(Exception e)
		{
			
		}
	}
	
	
	protected void closeConnection()
	{
		try{
			conn.disconnect();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	protected void savePreference(String username,String password)
	{

		int mode = Activity.MODE_PRIVATE;
		
		SharedPreferences mySharedPreferences = getSharedPreferences(PREFS, mode);
		SharedPreferences.Editor editor = 
		mySharedPreferences.edit();
		System.out.println("savepreference="+username+password);
		String userName = username;
		String passWord = password;
		
		


		editor.putString("USER_NAME",userName );
		editor.putString("PASS_WORD",password );
		editor.commit();
	}


	protected void loadPreference()
	{
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences mySharedPreferences = getSharedPreferences(PREFS, mode);
		
		userName_shared_prefernece= mySharedPreferences.getString("USER_NAME","UserName");
		passWord_shared_prefernece = mySharedPreferences.getString("PASS_WORD","Password");
		

	}
    
    
	protected void removePreference() {
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences mySharedPreferences = getSharedPreferences(PREFS, mode);
		 
		SharedPreferences.Editor editor = mySharedPreferences.edit(); 
				  editor.remove("USER_NAME"); 
				             editor.remove("PASS_WORD"); 
				             editor.commit(); 
	}
	
}